package de.hsmannheim.poolraumapp.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;

public class Rooms implements Iterable<Room> {
	private DateTime lastModified;
	private List<Room> rooms;
	private Map<String, Room> roomsByIdentifier;
	
	public Rooms() { }
	
	public Rooms(DateTime lastModified, List<Room> rooms) { 
		this.lastModified = lastModified;
		this.rooms = rooms;
	}
	
	public DateTime getLastModified() {
		return lastModified;
	}
	
	public List<Room> getRooms() {
		return rooms;
	}
	
	public Room getRoom(String roomIdentifier) {
		if (this.roomsByIdentifier == null) {
			Map<String, Room> roomsByIdentifier = new HashMap<String, Room>();
			for (Room room: rooms) {
				roomsByIdentifier.put(room.getIdentifier(), room);
			}
			this.roomsByIdentifier = roomsByIdentifier;
		}
		return this.roomsByIdentifier.get(roomIdentifier);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((lastModified == null) ? 0 : lastModified.hashCode());
		result = prime * result + ((rooms == null) ? 0 : rooms.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Rooms other = (Rooms) obj;
		if (lastModified == null) {
			if (other.lastModified != null)
				return false;
		} else if (!lastModified.equals(other.lastModified))
			return false;
		if (rooms == null) {
			if (other.rooms != null)
				return false;
		} else if (!rooms.equals(other.rooms))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Rooms [lastModified=" + lastModified + ", rooms=" + rooms + "]";
	}

	@Override
	public Iterator<Room> iterator() {
		return rooms.iterator();
	}
}
